const api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cookbookType: 1,
    lunch: {},
    dinner: {}
  },

  loadLunch() {
    api.get('/cookbook/today?cookbookType=1').then(res => {
      this.setData({
        lunch: res.data
      })
    })
  },

  loadDinner() {
    api.get('/cookbook/today?cookbookType=2').then(res => {
      this.setData({
        dinner: res.data
      })
    })
  },

  onChange (e) {
    this.setData({
      cookbookType: parseInt(e.detail.name)
    })
    this.loadCookbook()
  },

  loadCookbook () {
    switch (this.data.cookbookType) {
      case 1: 
      this.loadLunch()
      break
      case 2: 
      this.loadDinner()
      break
    }
  },

  addCart(e) {
    const food = e.currentTarget.dataset.food
    const cartAddBo = {
      foodId: food.id,
      cookbookType: this.data.cookbookType
    }
    api.post('/cart', cartAddBo).then(res => {
      console.log(res)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadCookbook()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadCookbook()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})